home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / bcshare.zip / BCSHARE.PAS < prev    next >
Pascal/Delphi Source File  |  1990-11-17  |  6KB  |  176 lines

  1. (*                      BCSHARE Beta Version 0.6                         *)
  2. (*                     Compliments of Mike Woltz                         *)
  3. (*                 Copyright (C) By Mike Woltz 1990                      *)
  4. (*                       Buffalo Creek Software                          *)
  5. (*                            A Member Of                                *)
  6. (*              The Association Of Shareware Professionals               *)
  7. (*               Home of SPITFIRE Bulletin Board System                  *)
  8. (*                                                                       *)
  9. (* BCSHARE is written by Mike Woltz (Buffalo Creek Software) to be used  *)
  10. (* to open files in a manner to provide the use of file sharing and      *)
  11. (* locking for multitasking and/or LAN systems.  BCSHARE is a written    *)
  12. (* in Turbo Pascal and interfaced with assembler code (BCSFILE.OBJ) for  *)
  13. (* speed.  You are free to use BCSHARE within your programs written in   *)
  14. (* Turbo Pascal provided Buffalo Creek Software is given credit for this *)
  15. (* code either in your program documentation or within your program      *)
  16. (* itself.  While you are free to use BCSHARE, you are NOT allowed to    *)
  17. (* alter this code.                                                      *)
  18.  
  19. {$I+,N-,V-,B-,S-,R-,L+,D+,F+}
  20. Unit BCShare;
  21. Interface
  22. Uses DOS;
  23.  
  24. CONST
  25.   Lock          : Byte = 0;
  26.   Unlock        : Byte = 1;
  27.   ReadMode      : Byte = 0;
  28.   WriteMode     : Byte = 1;
  29.   NormalMode    : Byte = 2;
  30.   ReadDenyNone  : Byte = 3;
  31.   WriteDenyNone : Byte = 4; 
  32.   DOS_Error            = $FFFF;
  33.  
  34. TYPE
  35.  ASCIIZString = Array[0..64] Of Char;
  36.  
  37. VAR
  38.   Regs      : Registers;
  39.   A         : ASCIIZString;
  40.   File_Pos  : LongInt;
  41.   IO_Error  : Word;
  42.  
  43. Procedure SetFileMode(Mode : Byte);
  44. Procedure StrToASCIIZ(S : String; VAR A : ASCIIZString);
  45. Function  OpenFile(A : ASCIIZString; FileMode : Byte) : Word;
  46. Procedure LockFile(Handle : Word; Mode : Byte; Start,Amount : LongInt);
  47. Function  CreateFile(A : ASCIIZString) : Word;
  48. Function  SeekFile(Handle : Word; Offset : LongInt; Method : Byte) : LongInt;
  49. Procedure WriteFile(Handle : Word; VAR Buffer; Bytes : Word);
  50. Function  ReadFile(Handle : Word; VAR Buffer; Bytes : Word) : Word;
  51. Procedure CloseFile(Handle : Word);
  52. Procedure Find_EOF(FHandle : Word);
  53. Procedure AssignTxtFile(VAR TxtF : Text; FileName : String);
  54.  
  55. Implementation
  56.  
  57. {$L BCSFILE.OBJ}
  58. Procedure SetFileMode(Mode : Byte);                                 External;
  59. Procedure StrToASCIIZ(S : String; VAR A : ASCIIZString);            External;
  60. Function  OpenFile(A : ASCIIZString; FileMode : Byte) : Word;       External;
  61. Procedure LockFile(Handle : Word; Mode : Byte;
  62.                    Start,Amount : LongInt);                         External;
  63. Function  CreateFile(A : ASCIIZString) : Word;                      External;
  64. Function  SeekFile(Handle : Word; Offset : LongInt;
  65.                    Method : Byte) : LongInt;                        External;
  66. Procedure WriteFile(Handle : Word; VAR Buffer; Bytes : Word);       External;
  67. Function  ReadFile(Handle : Word; VAR Buffer; Bytes : Word) : Word; External;
  68. Procedure CloseFile(Handle : Word);                                 External;
  69.  
  70. Procedure Find_EOF(FHandle : Word);
  71. VAR
  72.   CArray : Array[1..128] Of Char;
  73.   Wd,W    : Word;
  74.   Begin;
  75.     File_Pos:=SeekFile(FHandle,0,2);
  76.     Dec(File_Pos,1);
  77.     If File_Pos<0 Then Exit;
  78.     File_Pos:=File_Pos And $FFFF80;
  79.     File_Pos:=SeekFile(FHandle,File_Pos,0);
  80.     Wd:=ReadFile(FHandle,CArray,SizeOf(CArray));
  81.     W:=1;
  82.     While (W<=Wd) And (CArray[W]<>^Z) Do
  83.     Begin;
  84.       Inc(W);
  85.       Inc(File_Pos);
  86.     End;
  87.     File_Pos:=SeekFile(FHandle,File_Pos,0);
  88.   End;
  89.  
  90. Function TxtRead(VAR TxtF : TextRec) : Word;
  91.   Begin;
  92.     With TxtF Do
  93.     Begin;
  94.       BufEnd:=ReadFile(Handle,BufPtr^,BufSize);
  95.       BufPos:=0;
  96.       TxtRead:=0;
  97.     End;
  98.   End;
  99.  
  100. Function TxtWrite(VAR TxtF : TextRec) : Word;
  101.   Begin;
  102.     WriteFile(TxtF.Handle,TxtF.BufPtr^,TxtF.BufPos);
  103.     TxtF.BufPos:=0;
  104.     TxtF.BufEnd:=0;
  105.     TxtWrite:=0;
  106.   End;
  107.  
  108. Function TxtClose(VAR TxtF : TextRec):  Word;
  109.   Begin;
  110.     CloseFile(TxtF.Handle);
  111.     TxtF.BufPos:=0;
  112.     TxtF.BufEnd:=0;
  113.     TxtClose:=0;
  114.     SetFileMode(2);
  115.   End;
  116.  
  117. Function SetNull(VAR TxtF : TextRec):  Word;
  118.   Begin;
  119.     SetNull:=0;
  120.   End;
  121.  
  122. Function OpenTxt(VAR TxtF : TextRec) : Word;
  123. VAR
  124.   FName : String;
  125.   Begin;
  126.     TxtF.CloseFunc:=@TxtClose;
  127.     TxtF.FlushFunc:=@SetNull;
  128.     FName:=TxtF.Name;
  129.     StrToASCIIZ(FName,A);
  130.     If TxtF.Mode=FmInput Then
  131.     Begin;
  132.       SetFileMode(ReadDenyNone);
  133.       TxtF.Handle:=OpenFile(A,FileMode);
  134.       TxtF.InOutFunc:=@TxtRead;
  135.     End
  136.     Else 
  137.     If TxtF.Mode=FmOutput Then
  138.     Begin;
  139.       TxtF.Handle:=CreateFile(A);
  140.       TxtF.InOutFunc:=@TxtWrite;
  141.     End
  142.     Else
  143.     Begin;
  144.       SetFileMode(NormalMode);
  145.       TxtF.Handle:=OpenFile(A,FileMode);
  146.       If TxtF.Handle=DOS_Error Then TxtF.Handle:=CreateFile(A)
  147.       Else
  148.       Find_EOF(TxtF.Handle);
  149.       TxtF.Mode:=FmOutput;
  150.       TxtF.InOutFunc:=@TxtWrite;
  151.    End;
  152.    TxtF.BufPos:=0;
  153.    TxtF.BufEnd:=0;
  154.    If TxtF.Handle=DOS_Error Then OpenTxt:=IO_Error
  155.    Else
  156.    OpenTxt:=0;
  157.    SetFileMode(2);
  158. End;
  159.  
  160. Procedure AssignTxtFile(VAR TxtF : Text; FileName : String);
  161. VAR
  162.   I  : Integer;
  163.   TR : TextRec ABSOLUTE TxtF;
  164.   Begin;
  165.     With TR Do
  166.     Begin;
  167.       Handle:= $FFFF;
  168.       Mode:=FmClosed;
  169.       BufSize:=SizeOf(Buffer);
  170.       BufPtr:=@Buffer;
  171.       OpenFunc:=@OpenTxt;
  172.       StrToASCIIZ(FileName,A);
  173.       Move(A,Name,SizeOf(A));
  174.     End;
  175.   End;
  176. End.